from google.appengine.ext import webapp
#from DataClass.WebsiteRec import WebsiteRec
from google.appengine.api import users
from google.appengine.ext.webapp import template
from AdminDetailBase import AdminDetailBase
from PSData.PSInstitution import PSInstitution 
from DataClass.SiteUser import SiteUser

import os
import cgi 
import logging

class InstitutionDetailHandler(AdminDetailBase):
    def GetItemTemplateFileName(self):
        return "institutiondetail.html"
    
    def GetItem(self, key):
        return PSInstitution.get(key)
    
    def GetFormAction(self):
        return "institutiondetail"
    
    def GetDetailHeading(self):
        return "Institution"

    def PostIsAddNew(self, user):
        strKey = self.request.get('key')
        retval = len(strKey) == 0 
        return retval

    def ValidateAddNewPost(self, user):
        return True
        
    def ValidateUpdatePost(self, user):
        return True
    
    def PerformAddNewFromPost(self, user):
        siteuser = SiteUser.GetSiteUserFromEmail(user.email())
        institution = PSInstitution()
        institution.user = siteuser
        institution.archive = False
        retval = self.SetValuesFromPost(user, institution)
        return retval

    def PerformUpdateFromPost(self, user):
        strKey = "Key: " + self.request.get('key')
        logging.debug(strKey)
        institution = PSInstitution.get(self.request.get('key'))
        retval =  self.SetValuesFromPost(user, institution)
        return retval

    ###################
    
    def SetValuesFromPost(self, user, institution):
        institution.instname = self.request.get('instname', 'unnamed inst')
        institution.description = self.request.get('instdescription', 'no desc')
        institution.put()
        retval = str(institution.key())
        return retval
        